package com.example.administrator.adroidtest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import constant.CharacterType;
import parm.MDF;
import parm.MeasurementParam;

public class SpeedActivity extends Activity {
    private EditText wavelengthedit, upperfrequencylimitedit, lowerfrequencylimitedit;
    private CheckBox rmscheckbox, frequencyspectrum;
    private List<CheckBox> CheckBoxlist = new ArrayList<CheckBox>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_speed);
        initpage();
    }

    private void initpage() {
        wavelengthedit = (EditText) findViewById(R.id.wavelength_edit);
        upperfrequencylimitedit = (EditText) findViewById(R.id.upperfrequencylimit_edit);
        lowerfrequencylimitedit = (EditText) findViewById(R.id.lowerfrequencylimit_edit);
        rmscheckbox = (CheckBox) findViewById(R.id.rms_checkbox);
        frequencyspectrum = (CheckBox) findViewById(R.id.frequencyspectrum_checkbox);
        CheckBoxlist.add(rmscheckbox);
        CheckBoxlist.add(frequencyspectrum);
    }

    public void onClickspeedsave_bt(View view) {
        MeasurementParam mp = new MeasurementParam();
        if (mp instanceof Serializable) {
            /**
             * 获取Edittext的值
             */
            String wavelengthedittext = wavelengthedit.getText().toString();
            String upperfrequencylimitedittext = upperfrequencylimitedit.getText().toString();
            String lowerfrequencylimitedittext = lowerfrequencylimitedit.getText().toString();
            StringBuilder sb = new StringBuilder();

            /**
             * 获取多选框的值
             */
            for (CheckBox checkbox : CheckBoxlist) {
                if (checkbox.isChecked()) {
                    sb.append(checkbox.getText().toString() + " ");
                }

            }
            /**
             * 对得到值进行处理，添加到总的MeasurementParamManager的llst里面
             */
            if (sb != null && "".equals(sb.toString())) {
                Toast.makeText(getApplicationContext(), "please checkbox choice one!", Toast.LENGTH_SHORT).show();
            } else {

                Toast.makeText(getApplicationContext(), "you choice value" + wavelengthedittext + upperfrequencylimitedittext + lowerfrequencylimitedittext + sb.toString(), Toast.LENGTH_SHORT).show();
                int charc = CharacterType.AVERAGE;

                /**
                 *临时定义MDF对象放每一次的特征值对应的采集数据
                 */
                MDF mdf = new MDF();
                mdf.setCharacterValueType(charc);
                mdf.setDataLength(Integer.parseInt(wavelengthedittext));
                mdf.setUpLimitFrequency(Integer.parseInt(upperfrequencylimitedittext));
                mdf.setDownLimitFrequency(Integer.parseInt(lowerfrequencylimitedittext));

                Intent intent = new Intent();
                intent.setClass(SpeedActivity.this, MeasurementParamAddActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("mdfs", mdf);
                intent.putExtras(bundle);
                setResult(ResultCode.RESS_OK, intent);

            }

        }
        finish();
    }
}